package testAndSomeTries.videoDisplay;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;

public class PlayerVideo extends JFrame implements ControllerListener
{	
	private JPanel container = new JPanel();
	private MediaLocator _mediaL;
	private Player player = null;
	
	String saveURL = "";
	//Display parameters
	JButton buttonURL = new JButton("Choose");
	JButton buttonPlay = new JButton("Play");
	JLabel labelURL = new JLabel("Load a mpg file");
	JTextField textURL = new JTextField(null, 20);
	
	JPanel videoPanel = new JPanel();
	
	//Constructor
	public PlayerVideo(String nameVideo)
	{	
		super();
		this.setSize(500, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		//this.setResizable(false);		
		this.setContentPane(container);		
		this.setVisible(true);
		
		JPanel mainPanel = new JPanel();
		mainPanel.setBackground(Color.GREEN);
		
		mainPanel.setLayout(new GridBagLayout());
		GridBagConstraints topGBC = new GridBagConstraints();
		topGBC.gridx = 0;
		topGBC.gridy = 0;	
		mainPanel.add(labelURL, topGBC);
		
		topGBC.gridx = 1;
		topGBC.gridy = 0;	
		mainPanel.add(textURL, topGBC);
		
		topGBC.gridx = 2;
		topGBC.gridy = 0;			
		mainPanel.add(buttonURL, topGBC);
		
		topGBC.gridx = 1;
		topGBC.gridy = 1;			
		mainPanel.add(buttonPlay, topGBC);
		
		container.setLayout(new GridBagLayout());
		
		GridBagConstraints mainPanelGBC = new GridBagConstraints();		
		mainPanelGBC.fill = GridBagConstraints.HORIZONTAL;
		mainPanelGBC.gridx = 0;
		mainPanelGBC.gridy = 0;	
		mainPanelGBC.weightx = 1;
		container.add(mainPanel, mainPanelGBC);
		
		GridBagConstraints videoPanelGBC = new GridBagConstraints();	
		videoPanelGBC.gridx = 0;
		videoPanelGBC.gridy = 1;
		//videoPanelGBC.gridheight = 5;
		videoPanelGBC.weighty = 1;	
		videoPanelGBC.weightx = 1;	
		videoPanelGBC.fill = GridBagConstraints.BOTH;
		container.add(videoPanel, videoPanelGBC);
		
		//TO DO Menu
		
		
		ActionListener actionButtonURL = new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				saveURL = textURL.getText();
			}
		};
		
		ActionListener actionButtonPlay = new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				if(saveURL != null)
					videoFrame(saveURL);
			}
		};
		
		buttonURL.addActionListener(actionButtonURL);
		buttonPlay.addActionListener(actionButtonPlay);	
	}
	public void setMediaLocator(MediaLocator m){
		_mediaL = m;
	}
	private void videoFrame(String name)
	{
		//if(name.indexOf( ":") < 3) name = "file:" + name;
		name = "file:" + name;
		if(_mediaL == null){
			_mediaL = new MediaLocator(name);
		}
		try
		{
			//Read the file from the location name
			player = Manager.createPlayer(_mediaL);
			player.addControllerListener(this);
			//player go to realize state
			player.realize();
		}
		
		catch(Exception e)
		{
			System.out.println(e);
			System.out.println("Error creating player");
			return;
		}	
	}
	
	public void controllerUpdate(ControllerEvent ce)
	{
		if(ce instanceof RealizeCompleteEvent)
		{
			videoPanel.removeAll();
			
			//getVisualComponent() : show the video frames of the video
			Component vis = player.getVisualComponent();
			
			if(vis != null)
			{
				videoPanel.add(vis, BorderLayout.CENTER);
				videoPanel.setVisible(true);
				videoPanel.revalidate();
			}
			
			//player is playing
			player.start();
		}
		
		//End of the playing
//		else if (ce instanceof EndOfMediaEvent)
//		{				
//			if(player != null)
//			{
//				player.setMediaTime(new Time(0));
//				if (player.getTargetState() < Player.Started)
//				{
//					//prefetch : player ready to start
//					player.prefetch();
//				}
//				
//				//player is playing
//				player.start();
//			}
//		}
	}
	
	public static void main(String[] args)
	{
		PlayerVideo test = new PlayerVideo("plop4.mpg");
	}

}